package com.doodod.common.session;

import javax.servlet.http.HttpSession;
import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 * User: ydw
 * Date: 2015/5/6
 * Time: 15:33
 */
public class SessionContext {
    private static SessionContext seesionInstance;
    private HashMap sessionMap;

    // 单例
    private SessionContext() {
        sessionMap = new HashMap();
    }

    public static SessionContext getInstance() {
        if (seesionInstance == null) {
            seesionInstance = new SessionContext();
        }
        return seesionInstance;
    }

    public synchronized void AddSession(HttpSession session) {
        if (session != null) {
            sessionMap.put(session.getId(), session);
        }
    }

    public synchronized void DelSession(HttpSession session) {
        if (session != null) {
            sessionMap.remove(session.getId());
        }
    }

    public synchronized HttpSession getSession(String session_id) {
        if (session_id == null) return null;
        return (HttpSession) sessionMap.get(session_id);
    }

}
